package com.administrator.customviewtest.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2017/7/10.
 */

public class DateUtil {
    public static void getNextDay() {
        //当前时间
        Calendar cl = Calendar.getInstance();
        System.out.print("当前时间:");
        printCalendar(cl);
        //前一天
        cl = Calendar.getInstance();
        getBeforeDay(cl);
        System.out.print("前一天:");
        printCalendar(cl);
        //后一天
        cl = Calendar.getInstance();
        getAfterDay(cl);
        System.out.print("后一天:");
        printCalendar(cl);
    }

    public static List<String> getLastYearDay(int days) {
        List<String> list = new ArrayList<>();
        list.add("");
        for (int i = days; i >= 0; i--) {
            Calendar cl = Calendar.getInstance();
            cl.add(Calendar.DAY_OF_MONTH, -i);
            list.add(cl.get(Calendar.MONTH) + 1 + "/" + cl.get(Calendar.DATE));
        }
        return list;
    }
    public static List<String> getLastYearFiveDay(int days) {
        List<String> list = new ArrayList<>();
        list.add("");
        for (int i = days; i >= 0; i-=5) {
            Calendar cl = Calendar.getInstance();
            cl.add(Calendar.DAY_OF_MONTH, -i);
            list.add(cl.get(Calendar.MONTH) + 1 + "/" + cl.get(Calendar.DATE));
        }
        for (String s : list) {
            System.out.println(s);

        }
        return list;

    }

    public static List<String> getLastYearHourDay(int days) {
        List<String> list = new ArrayList<>();
        list.add("");
        List<String> tempList = Arrays.asList("04:00","08:00","12:00","6:00","20:00");
        for (int i = days; i >= 0; i--) {
            Calendar cl = Calendar.getInstance();
            cl.add(Calendar.DAY_OF_MONTH, -i);
            list.add(cl.get(Calendar.MONTH) + 1 + "/" + cl.get(Calendar.DATE));
            list.addAll(tempList);
        }
        for (String s : list) {
            System.out.println(s);

        }
        return list;
    }

    /**
     * 设置时间
     *
     * @param year
     * @param month
     * @param date
     * @return
     */
    public static Calendar setCalendar(int year, int month, int date) {
        Calendar cl = Calendar.getInstance();
        cl.set(year, month - 1, date);
        return cl;
    }

    /**
     * 获取当前时间的前一天时间
     *
     * @param cl
     * @return
     */
    private static Calendar getBeforeDay(Calendar cl) {
        //使用roll方法进行向前回滚
        //cl.roll(Calendar.DATE, -1);
        //使用set方法直接进行设置
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day - 1);
        return cl;
    }

    /**
     * 获取当前时间的后一天时间
     *
     * @param cl
     * @return
     */
    private static Calendar getAfterDay(Calendar cl) {
        //使用roll方法进行回滚到后一天的时间
        //cl.roll(Calendar.DATE, 1);
        //使用set方法直接设置时间值
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day + 1);
        return cl;
    }

    /**
     * 打印时间
     *
     * @param cl
     */
    public static void printCalendar(Calendar cl) {
        int year = cl.get(Calendar.YEAR);
        int month = cl.get(Calendar.MONTH) + 1;
        int day = cl.get(Calendar.DATE);
        System.out.println(year + "-" + month + "-" + day);
    }
}
